<?php

class parentt extends IgnitedRecord
{
	var $has_and_belongs_to_many = 'students';
	var $belongs_to = 'login';
	
	function parentt()
	{
        parent::IgnitedRecord();
    }
    
    function update($_POST, $id)
    {
        $this->db->where('id', $id);
        
        $val['firstname'] = $_POST['firstname'];
        $val['secondname'] = $_POST['secondname'];
        $val['telephone'] = $_POST['telephone'];
        $val['email'] = $_POST['email'];
        $val['address'] = $_POST['address'];
        $this->db->update('parentts', $val);

        $this->db->where('parentt_id', $id);
        $this->db->delete('parentts_students');
        foreach (explode(';', $_POST['student_id']) as $stid):
            if (strlen($stid) > 0):
                $this->db->insert('parentts_students', array('parentt_id' => $id, 'student_id' => $stid));
            endif;
        endforeach;
    }
    
    function get_related($id)
    {
        $this->db->where('parentt_id', $id);
        foreach ($this->db->get('parentts_students')->result() as $row):
            $student_id[] = $row->student_id;
        endforeach;
        $related['student_id'] = $student_id;
        
        foreach ($student_id as $sid):
            $this->db->where('id', $sid);
            foreach ($this->db->get('students')->result() as $row):
                $student[] = $row->firstname . " " . $row->secondname;
            endforeach;
        endforeach;
        $related['student'] = $student;
        
        return $related;
    }
}
?>
